package com.example.controller;

import com.example.entity.Merchant;
import com.example.entity.User;
import com.example.entity.indent;
import com.example.entity.menu;
import com.example.service.IndentService;
import com.example.service.MenuService;
import com.example.service.MerchantService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("merchant")
public class MerchantController {
      private static  final Logger log = LoggerFactory.getLogger(UserController.class);

      private MerchantService merchantService;
      private MenuService menuService;
      private IndentService indentService;

      private String loginID="";

      @Value("${spring.config.location}")
      private String realPath;

      public MerchantController(MerchantService merchantService, MenuService menuService, IndentService indentService) {
            this.merchantService = merchantService;
            this.menuService = menuService;
            this.indentService = indentService;
      }

      @RequestMapping("merchantID")
      public String merchantID(Merchant merchant, Model model){

//            自动生成账号且避免重复
            int randomID;
            do {
                  randomID = (int) (900000 * Math.random() + 100000);
            }while((!ObjectUtils.isEmpty(merchantService.findMerchantByID(randomID))));
            log.debug("random:{}",randomID);
            merchant.setId(randomID);
            model.addAttribute("merchantID",randomID);
            return "merchantRegister";
      }

      @RequestMapping("register")
      public String register(Merchant merchant){
            log.debug("用户名：{},真实姓名：{},密码：{},性别：{}",merchant.getMerchantName(),merchant.getRealName(),merchant.getMerchantPwd(),merchant.getSale());
            merchantService.register(merchant);
            return "redirect:/index";
      }

      @RequestMapping("login")
      public String login(String merchantName, String merchantPwd, Integer id,HttpSession session,Model model){

            log.debug("接收到的用户名{}，密码{}",merchantName,merchantPwd);
            try {
                  Merchant merchant=merchantService.login(merchantName,merchantPwd,id);
                  session.setAttribute("merchant",merchant);
                  model.addAttribute("merchant",merchant);
                  return "merchantIndex";
            }catch (Exception e){
                  e.printStackTrace();
                  return "redirect:/index";
            }
      }

      @RequestMapping("addMenu")
      public String addMenu(menu menu, MultipartFile img,Model model) throws IOException {
            log.debug("addMenu跳转成功");
            log.debug("接收信息：{}",menu.getShopName());
            log.debug("头像名称：{}",img.getOriginalFilename());
            log.debug("头像大小：{}",img.getSize());
            log.debug("上传的路径：{}",realPath);

            String originalFilename = img.getOriginalFilename();
            String fileNamePrefix = new SimpleDateFormat("yyyyMMddmmssSSS").format(new Date());
            String fileNameSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String fileName=fileNamePrefix+ fileNameSuffix;

            img.transferTo(new File(realPath,fileName));

//保存信息
            loginID=menu.getShopID();
            menu.setPhoto(fileName);
            menuService.save(menu);

            return menuList(menu,model);
      }

      @RequestMapping("menuList")
      public String menuList(menu menu,Model model){
            log.debug("%%%%%%删除后的商家ID：{}",loginID);
            List<menu> menuList=menuService.findAllMenus(loginID);
            model.addAttribute("menuLists",menuList);
            return "merchantMenuList";
      }

      @RequestMapping("deleteMenu")
      public String deleteMenu(Integer idpic,Model model,menu menu){
            log.debug("需要删除的菜品的ID：{}",idpic);

            String photo=menuService.findMenuByID(idpic).getPhoto();
//            删除信息
            menuService.deleteMenuByID(idpic);
//            删除照片
            new File(realPath,photo).delete();

            return menuList(menu,model);
      }

      @RequestMapping("menuDetail")
      public String menuDetail(Model model,Integer shopID){
            log.debug("接收到的商家ID：{}",shopID);
            List<menu> menuList=menuService.findAllMenus(String.valueOf(shopID));
            model.addAttribute("menuLists",menuList);
            loginID= String.valueOf(shopID);

            return "merchantMenuDetail";
      }

      @RequestMapping("merchantDetail")
      public String merchantDetail(Model model,Integer shopID){
            log.debug("接收到的商家ID：{}",shopID);
//            loginID= String.valueOf(shopID);
            Merchant merchantByID = merchantService.findMerchantByID(shopID);
            model.addAttribute("merchantByID",merchantByID);

            return "merchantDetail";
      }

      @RequestMapping("indetList")
      public String indetList(String merchantId,Model model){

            List<indent> indentList=indentService.findMerchantIndent(merchantId);

            model.addAttribute("indentList",indentList);

            return "merchantIndentList";
      }


      @RequestMapping("updateDetail")
      public String updateDetail(Merchant merchant,Integer id,Model model){
            log.debug("更新功能接收到的商家ID：{}",id);
            merchantService.updateMerchantByID(merchant);
            return merchantDetail(model,id);
      }

      @RequestMapping("updatePrice")
      public String updatePrice(Integer idpic,Integer id,Model model,menu menu){
            log.debug("调价功能接收到的商家ID：{}",id);
            log.debug("调价功能接收到的菜品ID：{}",idpic);

            menuService.updatePrice(menu);

            return menuList(menu,model);
      }

}
